package com.xg.daily.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xg.daily.model.bo.UserInfo;
import com.xg.daily.model.dto.UserInfoQueryDto;
import com.xg.daily.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/user/info")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestBody UserInfo userInfo) {
        try {
            userInfoService.save(userInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "添加数据成功";
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public String del(@RequestParam("id") String id) {
        userInfoService.removeById(id);
        return "删除成功";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody UserInfo userInfo) {
        userInfoService.saveOrUpdate(userInfo);
        return "更新成功";
    }

    @RequestMapping(value = "/query/name", method = RequestMethod.GET)
    @ResponseBody
    public List<UserInfo> queryByName(@RequestParam("name") String name) {
        List<UserInfo> userInfoList = null;
        try {
            userInfoList = userInfoService.list(new QueryWrapper<UserInfo>().eq("username", name));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return userInfoList;
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public UserInfo get(@RequestParam("id") Integer id) {
        UserInfo userInfo = null;
        try {
            userInfo = userInfoService.getById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return userInfo;
    }

    @RequestMapping(value = "/query/all", method = RequestMethod.POST)
    @ResponseBody
    public IPage<UserInfo> queryAll(@RequestBody UserInfoQueryDto queryDto) {
        Page<UserInfo> userInfoPage = null;
        try {
            if(!StringUtils.isBlank(queryDto.getUsername())){
                LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(UserInfo::getUsername, queryDto.getUsername());
                userInfoPage = userInfoService.page(new Page<>(queryDto.getPageIndex(), queryDto.getPageSize()), queryWrapper);
            }else {
                userInfoPage = userInfoService.page(new Page<>(queryDto.getPageIndex(), queryDto.getPageSize()));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return userInfoPage;
    }

}
